# -*- coding: utf-8 -*-
#
# Author:: hanjunfeng
# Date:: 2021/1/8
from xml.etree.ElementTree import fromstring

from submodules.common.lib.local_paths import get_test_data_path


def walkData(root_node, level, result_list):
    global unique_id
    temp_list = [unique_id, level, root_node.tag, root_node.attrib]
    result_list.append(temp_list)
    unique_id += 1

    # 遍历每个子节点
    children_node = root_node.getchildren()
    if len(children_node) == 0:
        return
    for child in children_node:
        walkData(child, level + 1, result_list)
    return


def walk_data(root_node, result_list):
    # todo 保存各级完整路径
    # current_xpath = current_xpath + root_node.tag
    # result_list.append(current_xpath)

    result_list.append(root_node.tag)

    # 遍历每个子节点
    # children_node = root_node.getchildren()
    children_node = list(root_node)

    if len(children_node) == 0:
        return
    for child in children_node:
        walk_data(child, result_list)
    return


def is_xml_structure_equal(xml_str1, xml_str2):
    tree1 = fromstring(xml_str1)
    result_list1 = []
    walk_data(tree1, result_list1)

    tree2 = fromstring(xml_str2)
    result_list2 = []
    walk_data(tree2, result_list2)

    return result_list1 == result_list2


if __name__ == "__main__":
    import os
    import time

    project_test_data_dir = get_test_data_path()
    xml_path1 = os.path.join(project_test_data_dir, "test3.xml")
    xml_path2 = os.path.join(project_test_data_dir, "test4.xml")
    xml_str1 = open(xml_path1, "r", encoding="utf-8").read()
    xml_str2 = open(xml_path2, "r", encoding="utf-8").read()
    t1 = time.time()
    res1 = is_xml_structure_equal(xml_str1, xml_str2)
    t2 = time.time()
    print(t2 - t1)
    # 0.004107952117919922
    t1 = time.time()
    res2 = xml_str1 == xml_str2
    t2 = time.time()
    print(t2 - t1)
    # 1.1920928955078125e-06
    print(res1)
    print(res2)
